/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import dao.DonDatHangDAO;
import java.util.ArrayList;
import java.util.List;
import pojo.ChiTietDonDatHang;
import pojo.DonDatHang;
import pojo.SanPham;
import service.DonDatHangService;

/**
 *
 * @author MongKieu
 */
public class DonDatHangServiceImpl implements DonDatHangService {

    private DonDatHangDAO donDatHangDAO;

    public DonDatHangDAO getDonDatHangDAO() {
        return donDatHangDAO;
    }

    public void setDonDatHangDAO(DonDatHangDAO donDatHangDAO) {
        this.donDatHangDAO = donDatHangDAO;
    }

    @Override
    public void saveDonDatHang(DonDatHang donDatHang) {
        donDatHangDAO.saveDonDatHang(donDatHang);
    }

    @Override
    public void saveDonDatHang(DonDatHang donDatHang, ArrayList<SanPham> gioHang) {
        List<ChiTietDonDatHang> listChiTietDonHang = new ArrayList<ChiTietDonDatHang>();
        float tongGiaTien = 0;
        for (int h = 0; h < gioHang.size(); h++) {

            SanPham sanPham = gioHang.get(h);
            ChiTietDonDatHang chiTietDonDatHang = new ChiTietDonDatHang();
            chiTietDonDatHang.setSanPham(sanPham);
            chiTietDonDatHang.setDonGia(sanPham.getGiaBanHienTai());
            chiTietDonDatHang.setSoLuong(sanPham.getSoLuongDangCo());
            chiTietDonDatHang.setDonDatHang(donDatHang);
            listChiTietDonHang.add(chiTietDonDatHang);
            tongGiaTien = tongGiaTien + sanPham.getGiaBanHienTai() * sanPham.getSoLuongDangCo();
        }
        donDatHang.setTongGiaTri(tongGiaTien);
        donDatHang.setDanhSachChiTietDonDatHang(listChiTietDonHang);
        saveDonDatHang(donDatHang);
    }
}
